<?php
import('@.Action.PublicAction');
class PlanquotaAction extends Action
{
    function _initialize() 
    {
       if(!isset($_SESSION['user_right'])) {
			$this->redirect('login','Public');
        }
        parent::_initialize();
    }
    	
	function index()
	{
		if(empty($_REQUEST['PLAN_ID'])) $_REQUEST['PLAN_ID']=$_SESSION["PLAN_ID"];	
		else $_SESSION["PLAN_ID"]=$_REQUEST['PLAN_ID'];
		//print_r($_SESSION["PLAN_ID"]);
		//列表过滤器，生成查询Map对象
        $map = $this->_search('Plan_quota');		
        if(method_exists($this,'_filter')) {
            $this->_filter($map);
        }
		$model        = D('Plan_quota');
        if(!empty($model)) {
        	$this->_list($model,$map);
        }
		$Dao =D('Plan');
		$plan=$Dao->find($_REQUEST['PLAN_ID']);
		$this->assign("plan",       $plan);
		$this->display();
        return;
		
	}
    /**
     +----------------------------------------------------------
     * 根据表单生成查询条件
     * 进行列表过滤
     +----------------------------------------------------------
     * @access protected 
     +----------------------------------------------------------
     * @param string $name 数据对象名称 
     +----------------------------------------------------------
     * @return HashMap
     +----------------------------------------------------------
     * @throws ThinkExecption
     +----------------------------------------------------------
     */
    protected function _search($name='') 
    {
        //生成查询条件
		if(empty($name)) {
			$name	=	$this->name;
		}
		$model	=	D($name);
		$map	=	array();
/*		foreach($model->getDbFields() as $key=>$val) {
            if($_REQUEST['searchKey']==$val) {
                //原来的==查询
            	$map[$val]	=	$_REQUEST['searchValue'];
            }
        }*/
		if( $_POST["searchKey"] == "XH" ) $map["XH"]	=	$_POST["searchValue"];
		if( $_POST["searchKey"] == "XM" ) $map["XM"]	=	$_POST["searchValue"];
		if($_SESSION["PLAN_ID"]) $map["PLAN_ID"]	=	$_SESSION["PLAN_ID"];
		if($_SESSION["dm_yx"]) $map["DM_YX"]	=	$_SESSION["dm_yx"];
		if($_SESSION["dm_zy"]) $map["DM_ZY"]	=	$_SESSION["dm_zy"];
		if($_SESSION["dm_bj"]) $map["DM_BJ"]	=	$_SESSION["dm_bj"];				
        return $map;
    }	
    /**
     +----------------------------------------------------------
     * 根据表单生成查询条件
     * 进行列表过滤
     +----------------------------------------------------------
     * @access protected 
     +----------------------------------------------------------
     * @param Model $model 数据对象 
     * @param HashMap $map 过滤条件 
     * @param string $sortBy 排序 
     * @param boolean $asc 是否正序 
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     * @throws ThinkExecption
     +----------------------------------------------------------
     */
    protected function _list($model,$map,$sortBy='',$asc=true) 
    {
        //排序字段 默认为主键名
        if(isset($_REQUEST['order'])) {
            $order = $_REQUEST['order'];
        }else {
            $order = !empty($sortBy)? $sortBy: $model->getPk();
        }
        //排序方式默认按照倒序排列
        //接受 sost参数 0 表示倒序 非0都 表示正序
        if(isset($_REQUEST['sort'])) {
            $sort = $_REQUEST['sort']?'asc':'desc';
        }else {
            $sort = $asc?'asc':'desc';
        }

        //dump($map);
		/*添加HashMap来修改成模糊查询 link %%*/
		import("ORG.Util.HashMap");
		$maptmp=new HashMap();
        /**/
		foreach ($map as $key=>$val){
			$maptmp->put($key,array('like',"%".$val."%"));

		}
		//取得满足条件的记录数
        $count      = $model->count($maptmp);
		if($count>0) {
			import("ORG.Util.Page");
			//创建分页对象
			if(!empty($_REQUEST['listRows'])) {
				$listRows  =  $_REQUEST['listRows'];
			}else {
				$listRows  =  '';
			}
			$p          = new Page($count,$listRows);
			//分页查询数据
			$voList     = $model->findAll($maptmp,'*',$order.' '.$sort,$p->firstRow.','.$p->listRows);
			//$voList     = $model->findAll("xh like '%200555%'",'*',$order.' '.$sort,$p->firstRow.','.$p->listRows);
			//分页跳转的时候保证查询条件
			foreach($map as $key=>$val) {
				if(is_array($val)) {
					foreach ($val as $t){
						$p->parameter	.= $key.'[]='.urlencode($t)."&";
					}
				}else{
					$p->parameter   .=   "$key=".urlencode($val)."&";        
				}
			}
			//分页显示
			$page       = $p->show();
			//列表排序显示
			$sortImg    = $sort ;                                   //排序图标
			$sortAlt    = $sort == 'desc'?'升序排列':'倒序排列';    //排序提示
			$sort       = $sort == 'desc'? 1:0;                     //排序方式
			//模板赋值显示
			$this->assign('list',       $voList);
			$this->assign('sort',       $sort);
			$this->assign('order',      $order);
			$this->assign('sortImg',    $sortImg);
			$this->assign('sortType',   $sortAlt);
			$this->assign("page",       $page);
		}
        return ;
    }
	

    /**
     +----------------------------------------------------------
     * 默认删除操作
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     * @return string
     +----------------------------------------------------------
     * @throws ThinkExecption
     +----------------------------------------------------------
     */
    public function delete()
    {
        //删除指定记录
        $model        = D('Plan_quota');
        if(!empty($model)) {
            $id         = $_REQUEST['id'];
            if(isset($id)) {
                if($model->delete($id)){
                    $this->success(L('_DELETE_SUCCESS_'));
                }else {
                    $this->error(L('_DELETE_FAIL_'));
                }        	
            }else {
                $this->error('非法操作');
            }        	
        }
    }


	
	function add() 
	{ 
		// 添加内容的页面
		$Dao =D('Plan_option');
		$voList=$Dao->findAll("`PLAN_ID`='".$_SESSION['PLAN_ID']."'");
		if(!$voList){
			header('Content-Type: text/html; charset=UTF-8');
			echo "<script>alert('没有子项目可以增加!请联系发起该项目的用户!');window.close();</script>";
			exit();
		}		
		$this->assign("list",$voList);  
		$Dao = D("CodeView");
		import("ORG.Util.HashMap");
		//院系
		$maptmp=new HashMap();
		$maptmp->put("Comment.TABLE",array('eq',"studentinfo"));		
		$maptmp->put("Comment.FIELD",array('eq',"dm_yx"));
		$voyList = $Dao->findAll($maptmp);
		$this->assign("yxlist",$voyList);
		
		if($_SESSION['user_right']==6)
		{
				//专业
			$maptmp=new HashMap();
			$maptmp->put("Comment.TABLE",array('eq',"studentinfo"));		
			$maptmp->put("Comment.FIELD",array('eq',"dm_zy"));
			$maptmp->put("Code.PCODE",array('eq',$_SESSION['dm_yx']));			
			$vozList = $Dao->findAll($maptmp);
			$this->assign("zylist",$vozList);
			//班级
			$maptmp=new HashMap();
			$maptmp->put("Comment.TABLE",array('eq',"studentinfo"));		
			$maptmp->put("Comment.FIELD",array('eq',"dm_bj"));
			$maptmp->put("Code.PCODE",array('eq',$_SESSION['dm_zy']));				
			$vobList = $Dao->findAll($maptmp);
			$this->assign("bjlist",$vobList);
			$Right=D("Uright");
			$vo=$Right->find("`USER_ID`='".$_SESSION['USER_ID']."' and `DM_BJ`='".$_SESSION['dm_bj']."'");
			$this->assign("vo",$vo);

		}
 		
		$this->display(); 
	} 

	function insert() 
	{ 
		// 保存添加的数据
		$Dao = D("Plan_quota"); 
		$vo = $Dao->create(); 
		if(false === $vo) { 
	 		$this->error($Dao->getError()); 
		} 
		$_POST['num']=intval($_POST['num']);
 		for($i=0;$i<$_POST['num'];$i++)
		{
			$Dao->OPTION_NAME=$_POST['OPTION_NAME'];
			$rs = $Dao->add(); 
		}
		if($rs){ 
			$this->assign("jumpUrl",__URL__); 
			$this->success("添加数据成功!"); 
		}else{ 
			$this->error("添加数据失败"); 
		} 
	}
	function edit() 
	{ 
		//检查是否时间或者状态可用
		$Dao = D("Plan"); 
		$vo = $Dao->find($_SESSION["PLAN_ID"]); 
		if($vo['PLAN_STATUS']!=1){
			header('Content-Type: text/html; charset=UTF-8');
			echo "<script>alert('状态锁定了!');window.close();</script>";
			exit();
		}
		if(strtotime($vo['PLAN_STIME'])>time()){
			header('Content-Type: text/html; charset=UTF-8');
			echo "<script>alert('还没有开始!');window.close();</script>";
			exit();
		}
		if((strtotime($vo['PLAN_ETIME'])+86400)<time()){
			header('Content-Type: text/html; charset=UTF-8');
			echo "<script>alert('时间过期了!');window.close();</script>";
			exit();
		}		
		// 编辑页面 
		$Dao = D("Plan_quota"); 
		$vo = $Dao->find($_GET["id"]); 
		if($vo === false) 
		{ 
			$this->assign("jumpUrl",__URL__); 
			$this->error("Non-existed record"); 
		}
		$this->assign("vo",$vo);	
		$Dao = D("CodeView");
		import("ORG.Util.HashMap");
		//院系
		$maptmp=new HashMap();
		$maptmp->put("Comment.TABLE",array('eq',"studentinfo"));		
		$maptmp->put("Comment.FIELD",array('eq',"dm_yx"));
		$voyList = $Dao->findAll($maptmp);
		$this->assign("yxlist",$voyList); 
		//专业
		$maptmp=new HashMap();
		$maptmp->put("Comment.TABLE",array('eq',"studentinfo"));		
		$maptmp->put("Comment.FIELD",array('eq',"dm_zy"));
		$maptmp->put("Code.PCODE",array('eq',$vo['DM_YX']));			
		if($vo['DM_YX'])$vozList = $Dao->findAll($maptmp);
		$this->assign("zylist",$vozList);
		//班级
		$maptmp=new HashMap();
		$maptmp->put("Comment.TABLE",array('eq',"studentinfo"));		
		$maptmp->put("Comment.FIELD",array('eq',"dm_bj"));
		$maptmp->put("Code.PCODE",array('eq',$vo['DM_ZY']));				
		if($vo['DM_ZY'])$vobList = $Dao->findAll($maptmp);
		$this->assign("bjlist",$vobList);
		//学生
		$Dao = D("Studentinfo");
		$maptmp=new HashMap();		
		$maptmp->put("dm_bj",array('eq',$vo['DM_BJ']));		
		if($vo['DM_BJ'])$vosList = $Dao->findAll($maptmp);
		$this->assign("stlist",$vosList);				 		 
		$this->display(); 
	} 

	function update() 
	{ 
		// 保存编辑过的数据 
		$Dao = D("Plan_quota"); 
		//防止重复评奖
		if(!empty($_POST['XH']))
		{
			$vo=$Dao->find("`PLAN_ID`='".$_POST['PLAN_ID']."' AND `OPTION_NAME`='".$_POST['OPTION_NAME']."' AND `XH`='".$_POST['XH']."'");
			if($vo) {
				$Model = new Model(); // 实例化一个model对象 没有对应任何数据表
				$sql = "UPDATE `xtu_plan_quota` "
						. "SET `BZ`='".$_POST['BZ']."'"
						. " WHERE `PLAN_ID`='".$_POST['PLAN_ID']."' AND `OPTION_NAME`='".$_POST['OPTION_NAME']."' AND `XH`='".$_POST['XH']."'";
				$Model->execute($sql);
				$this->success("备注保存成功!"); 
				exit();
			}
			$St = D("Studentinfo");
			$v = $St->find("`xh`='".$_POST['XH']."'");
			$_POST['SFZH'] = $v['sfzh'];
		}
		$vo = $Dao->create("","edit");
		if(false === $vo) { 
	 		$this->error($Dao->getError()); 
		} 
 
		$rs = $Dao->save(); 
		if($rs){ 
			$this->assign("jumpUrl",__URL__); 
			$this->success("保存修改成功!"); 
		}else{ 
			$this->error("保存修改失败"); 
		} 
	}
	//判断学生是否相同
	function existStudent(){
		// 保存编辑过的数据 
		$Dao = D("Plan_quota"); 
		//防止重复评奖
		if(!empty($_GET['XH']))
		{
			$vo=$Dao->find("`PLAN_ID`='".$_GET['PLAN_ID']."' AND `OPTION_NAME`='".$_POST['OPTION_NAME']."' AND `XH`='".$_GET['XH']."'");
			if($vo) $this->ajaxReturn('该学生已经评过了!','数据加载成功！',1);
			else $this->ajaxReturn('','数据加载成功！',1);
		}
	}
	//加载专业信息
	function selectZy(){
		$Dao = D("CodeView");
		import("ORG.Util.HashMap");
		$maptmp=new HashMap();
		$maptmp->put("Comment.TABLE",array('eq',"studentinfo"));		
		$maptmp->put("Comment.FIELD",array('eq',"dm_zy"));
		$maptmp->put("Code.PCODE",array('eq',$_GET['yx']));		
		$voList = $Dao->findAll($maptmp);
		foreach ($voList as $tmp)
		{
			$vo[]=array('value'=>$tmp['CODE'],'text'=>$tmp['CODENAME']);
		}
		$this->ajaxReturn($vo,'数据加载成功！',1);
	}
	//加载班级信息
	function selectBj(){
		$Dao = D("CodeView");
		import("ORG.Util.HashMap");
		$maptmp=new HashMap();
		$maptmp->put("Comment.TABLE",array('eq',"studentinfo"));		
		$maptmp->put("Comment.FIELD",array('eq',"dm_bj"));
		$maptmp->put("Code.PCODE",array('eq',$_GET['zy']));		
		$voList = $Dao->findAll($maptmp);
		foreach ($voList as $tmp)
		{
			$vo[]=array('value'=>$tmp['CODE'],'text'=>$tmp['CODENAME']);
		}
		$this->ajaxReturn($vo,'数据加载成功！',1);
	}
	//加载学生信息
	function selectStudent(){
		$Dao = D("Studentinfo");
		import("ORG.Util.HashMap");
		$maptmp=new HashMap();
		$maptmp->put("dm_bj",array('eq',$_GET['bj']));		
		$voList = $Dao->findAll($maptmp);
		foreach ($voList as $tmp)
		{
			$vo[]=array('value'=>$tmp['xh'],'text'=>$tmp['xm']);
		}
		$this->ajaxReturn($vo,'数据加载成功！',1);
	}
	function exportData() {
		set_time_limit(0);
		
		//使用哪个模型和表字段注释
		$modelName="Plan_quota";
		//准备导出数据
		$model= D($modelName);
        $map = $this->_search('Plan_quota');	//列表过滤器，生成查询Map对象 		
		import("ORG.Util.HashMap");//map中的查询条件导出
		$maptmp=new HashMap();
        /*map中的查询条件导出*/
		foreach ($map as $key=>$val){
			$maptmp->put($key,array('like',"%".$val."%"));
		}	
		$list  = $model->findAll($maptmp,'*','');


		
		//准备字段和长度,字段中文名称
		//	$fieldsize[]=$tmp['LENGTH'];
		//	$fieldname[]=$tmp['DBFNAME'];
		//	$fieldDm[]=$tmp['FIELD'];
			$fieldsize[]=40;
			$fieldname[]="奖项";
			$fieldDm[]="OPTION_NAME";
						
			$fieldsize[]=20;
			$fieldname[]="院系";
			$fieldDm[]="YX";
			
			$fieldsize[]=20;
			$fieldname[]="专业";
			$fieldDm[]="ZY";

			$fieldsize[]=40;
			$fieldname[]="班级";
			$fieldDm[]="BJ";

			$fieldsize[]=20;
			$fieldname[]="姓名";
			$fieldDm[]="XM";
			
			$fieldsize[]=20;
			$fieldname[]="学号";
			$fieldDm[]="XH";

			$fieldsize[]=20;
			$fieldname[]="身份证";
			$fieldDm[]="SFZH";
			
			$fieldsize[]=60;
			$fieldname[]="备注";
			$fieldDm[]="BZ";																			
		//准备导出数据
		foreach($list as $tmp1){
			$arr="";
			foreach($fieldDm as $tmp2){
				$arr[]=$tmp1[$tmp2];
				//echo $tmp1[$tmp2]."<br>";
			}
			$field[]=$arr;
		}
		//清除过期的缓存文件(就是两分钟以前的)
		$files=glob("../Public/Uploads/*.dbf");
		foreach($files as $v){
			if(time()>(filemtime($v)+1200)){
				@unlink($v);
			}
		}
		
		//导入DBF类
        import("ORG.Io.Dbf");
		//准备数据，字段和字段大小
		//$field=array(array("2005551033","无问林","男"),array("2005551034","房磊","男"));
		
/*		$arr="";
		$arr[]="2005551033";
		$arr[]="无问林";
		$arr[]="男";
		$field[]=$arr;
		
		$arr="";
		$arr[]="2005551033";
		$arr[]="无问林";
		$arr[]="男";
		$field[]=$arr;
		
		$fieldsize[]=20;
		$fieldsize[]=30;
		$fieldsize[]=2;
		//$fieldsize=array(20,30,2);
		$fieldname[]="无问林和房11";
		$fieldname[]="无问林房22";
		$fieldname[]="无问林房33";*/
		//$fieldname=array("无问林房11","无问林房22","无问林房33");
		//写DBF文件

		$outfile=new outdbf('../Public/Uploads/'.time().".dbf",$field,$fieldsize,$fieldname,1);/*发送响应*/
		header('Content-Type: text/html; charset=UTF-8');	
		echo "<div align='center'><a href='http://".$_SERVER['HTTP_HOST']."/".str_replace("../","",$outfile->file)."'>下载地址</a></div>";


	}
				
}
?>